package com.epoint.androidphone.mobileoa.ui.webinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.androidmobile.bizlogic.webinfo.db.WebInfoDb;
import com.epoint.androidmobile.bizlogic.webinfo.model.InfoListModel;
import com.epoint.androidmobile.bizlogic.webinfo.task.WebInfo_GetView_Task;
import com.epoint.androidmobile.core.string.StringHelp;
import com.epoint.androidmobile.core.xml.XMLUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;

/**
 * @author liyc
 * @time 2013-5-8 上午11:23:45
 * @annomation 信息简报列表
 */
public class WebInfoListView extends SuperPhonePage {
	
	String categoryguid;
	int isTzgg;
	int pageindex = 1;
	int pagesize = 20;
	long getListTaskId;
	List<InfoListModel> list = new ArrayList<InfoListModel>();
	LstAdapter adapter = new LstAdapter();
	ListView lv;
	ImageView ivQuery;
	EditText etCondition;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.todolistview, getIntentViewTitle());
		categoryguid = getIntent().getStringExtra("categoryguid");
		isTzgg = getIntent().getIntExtra("isTzgg", 0);
		if(isTzgg==1)
		{
			categoryguid = WebInfoDb.getNoticeCategoryguid(this);
			System.out.println("通知公告："+categoryguid);
		}
		ivQuery = (ImageView) findViewById(R.id.ivQuery);
		ivQuery.setOnClickListener(this);
		etCondition = (EditText) findViewById(R.id.etCondition);
		lv = (ListView) findViewById(R.id.lv);
		lv.setOnItemClickListener(this);
		addFoot();
		lv.setAdapter(adapter);
		
		getData(pageindex, pagesize, "");
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO 自动生成的方法存根
		super.onItemClick(parent, view, position, id);
		if(position==list.size()){
			pageindex++;
			getData(pageindex, pagesize, etCondition.getText().toString());
			return;
		}
		else
		{
			Intent intent = new Intent(this, WebInfoDetailView.class);
			intent.putExtra("InfoGuid", list.get(position).InfoGuid);
			intent.putExtra("Title", list.get(position).txtTitle);
			intent.putExtra("PostUserName", list.get(position).PostUserName);
			intent.putExtra("PostDate", list.get(position).PostDate);
			startActivity(intent);
		}
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		if(v==ivQuery)
		{
			list.clear();
			pageindex = 1;
			if(lv.getFooterViewsCount()==0){
				addFoot();
			}
			lv.setVisibility(View.INVISIBLE);
			getData(pageindex, pagesize,etCondition.getText().toString());
		}
	}
	
	private LinearLayout foot;
	public void addFoot(){
		TextView load_bt = new TextView(this);
		load_bt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
		load_bt.setText("更多...");
		load_bt.setGravity(Gravity.CENTER);
		foot = new LinearLayout(this);
		foot.addView(load_bt, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				55));
		foot.setGravity(Gravity.CENTER);
		lv.addFooterView(foot);
		registerForContextMenu(lv);
	}

	public void getData(int _pageindex,int _pagesize,String _keyWord)
	{
		showTopProgressBar();
		HashMap<String, Object> params = getPassMap();
		params.put("CurrentPageIndex", _pageindex);
		params.put("PageSize", _pagesize);
		params.put("CategoryGuid", categoryguid);
		params.put("KeyWord", _keyWord);
		getListTaskId = new WebInfo_GetView_Task(this, params).startTask();
	}
	
	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if(taskId==getListTaskId)
		{
			if(validateXML(obj))
			{
				lv.setVisibility(View.VISIBLE);
				String bs = obj.toString();
				String InfoListXml = StringHelp.getXMLAttOut(bs, "InfoList");
				List<InfoListModel> listtemp = XMLUtil.DomAnalysisCommon(InfoListXml, InfoListModel.class);
				
				for(InfoListModel a:listtemp)
				{
					list.add(a);
				}
				adapter.notifyDataSetChanged();
				
				if(listtemp.size()<pagesize)
				{
					lv.removeFooterView(foot);
				}
				
				adapter.notifyDataSetChanged();
			}
		}
	}
	
	class LstAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			LayoutInflater mInflater = LayoutInflater.from(getContext());
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.noticelistlayout, null);
				holder.tvTitle = (TextView) convertView
						.findViewById(R.id.tvTitle);
				holder.tvSender = (TextView) convertView
						.findViewById(R.id.tvSender);
				holder.tvTime = (TextView) convertView
						.findViewById(R.id.tvTime);
				holder.ivStatus = (ImageView) convertView
						.findViewById(R.id.ivStatus);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			InfoListModel a = list.get(position);

			holder.tvTitle.setText(a.txtTitle);
			holder.tvSender.setText(a.PostUserName);
			holder.tvTime.setText(a.PostDate.split(" ")[0]);
			if(a.IsRead.equals("0")){
				holder.ivStatus.setVisibility(View.VISIBLE);
			}
			else{
				holder.ivStatus.setVisibility(View.GONE);
			}
			return convertView;
		}
		public class ViewHolder {
			public TextView tvTitle;
			public TextView tvSender;
			public TextView tvTime;
			public ImageView ivStatus;
		}
	}
}
